package com.example.stock.service.calc;

import com.example.stock.util.CalcUtil;
import com.example.stock.util.ValidUtil;
import com.example.stock.vo.DataVo;
import com.example.stock.vo.EmaTradeVo;
import com.example.stock.vo.MaTradeVo;
import com.example.stock.vo.common.TradeVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhuchunfeng
 * @Description
 * @Date: 2021/1/3 15:37
 * Copyright (c) 2019 北京新媒传信科技有限公司
 */
@Service
public class CalcService {

    public List<TradeVo> calcMaCrossTrade(List<DataVo> closeData, int m, int n) {
        List<DataVo> mDataList= CalcUtil.MA(closeData, m);
        List<DataVo> nDataList = CalcUtil.MA(closeData, n);

        List<TradeVo> emaTradeVos = new ArrayList<>();
        for (int i = 0; i < closeData.size(); i++) {
            DataVo dataVo = closeData.get(i);
            DataVo mData = mDataList.get(i);
            DataVo nData = nDataList.get(i);

            if (!(dataVo.getTime().equals(mData.getTime()))  || !(dataVo.getTime().equals(nData.getTime()))) {
                throw new RuntimeException("计算出错!! location: com.example.stock.service.StockStatService.calcMaTrade");
            }
            TradeVo tradeVo = new TradeVo();
            tradeVo.setTime(dataVo.getTime());
            tradeVo.setPrice(dataVo.getBigDecimal());
            tradeVo.setCanBuy(mData.getBigDecimal().compareTo(nData.getBigDecimal()) <= 0);
            emaTradeVos.add(tradeVo);
        }
        ValidUtil.validTimeTrade(emaTradeVos);
        return emaTradeVos;
    }

    public List<TradeVo> calcEmaAdjustTrade(List<DataVo> closeData, int m, int n, BigDecimal refer) {
        List<DataVo> _san_Map = get_san_Map(closeData, m, n);
        List<DataVo> upDownMap = CalcUtil._JIAN(_san_Map, CalcUtil.REF(_san_Map, 1));

        List<TradeVo> emaTradeVos = new ArrayList<>();
        for (int i = 0; i < closeData.size(); i++) {
            DataVo dataVo = closeData.get(i);
            DataVo jj = _san_Map.get(i);
            DataVo _san_ = upDownMap.get(i);
            if (!(dataVo.getTime().equals(jj.getTime()) && dataVo.getTime().equals(_san_.getTime()))) {
                throw new RuntimeException("计算出错!!  location: com.example.stock.service.StockStatService.calcEmaTrade");
            }
            TradeVo emaTradeVo = new EmaTradeVo();
            emaTradeVo.setTime(dataVo.getTime());
            emaTradeVo.setPrice(dataVo.getBigDecimal());
            emaTradeVo.setCanBuy(_san_.getBigDecimal().compareTo(BigDecimal.ZERO) > 0
                    || jj.getBigDecimal().compareTo(refer) > 0);
            emaTradeVos.add(emaTradeVo);
        }
        ValidUtil.validTimeTrade(emaTradeVos);
        return emaTradeVos;
    }

    public List<TradeVo> calcEmaTrade(List<DataVo> closeData, int m, int n) {
        List<DataVo> _san_Map = get_san_Map(closeData, m, n);
        List<DataVo> upDownMap = CalcUtil._JIAN(_san_Map, CalcUtil.REF(_san_Map, 1));

        List<TradeVo> emaTradeVos = new ArrayList<>();
        for (int i = 0; i < closeData.size(); i++) {
            DataVo dataVo = closeData.get(i);
            DataVo _san_ = _san_Map.get(i);
            DataVo upDown = upDownMap.get(i);
            if (!(dataVo.getTime().equals(_san_.getTime()) && dataVo.getTime().equals(upDown.getTime()))) {
                throw new RuntimeException("计算出错!! location: com.example.stock.service.StockStatService.calcEmaTrade");
            }
            EmaTradeVo emaTradeVo = new EmaTradeVo();
            emaTradeVo.setTime(dataVo.getTime());
            emaTradeVo.setPrice(dataVo.getBigDecimal());
            emaTradeVo.setJj(_san_.getBigDecimal());
            emaTradeVo.setSan_map(upDown.getBigDecimal());
            emaTradeVo.setCanBuy(emaTradeVo.getSan_map().compareTo(BigDecimal.ZERO) > 0);
            emaTradeVos.add(emaTradeVo);
        }
        ValidUtil.validTimeTrade(emaTradeVos);
        return emaTradeVos;
    }

    public List<TradeVo> calcMaTrade(List<DataVo> closeData, int n) {
        List<DataVo> maDataVos = CalcUtil.MA(closeData, n);
        List<TradeVo> emaTradeVos = new ArrayList<>();
        for (int i = 0; i < closeData.size(); i++) {
            DataVo dataVo = closeData.get(i);
            DataVo maData = maDataVos.get(i);

            if (!(dataVo.getTime().equals(maData.getTime()))) {
                throw new RuntimeException("计算出错!! location: com.example.stock.service.StockStatService.calcMaTrade");
            }
            MaTradeVo maTradeVo = new MaTradeVo();
            maTradeVo.setTime(dataVo.getTime());
            maTradeVo.setPrice(dataVo.getBigDecimal());
            maTradeVo.setMa(maData.getBigDecimal());
            maTradeVo.setOffset(dataVo.getBigDecimal().subtract(maData.getBigDecimal()));
            maTradeVo.setCanBuy(maTradeVo.getOffset().compareTo(BigDecimal.ZERO) > 0);
            emaTradeVos.add(maTradeVo);
        }
        ValidUtil.validTimeTrade(emaTradeVos);
        return emaTradeVos;
    }

    public List<DataVo> get_san_Map(List<DataVo> closeMap, int m, int n) {
        // CLOSE    MA(CLOSE,m)         MA(CLOSE,M)
        return CalcUtil.EMA(CalcUtil._CHU ( CalcUtil._CHENG( CalcUtil._JIAN(closeMap, CalcUtil.MA(closeMap, m)) ,1000) , CalcUtil.MA(closeMap, m)),  n);
    }
}
